package session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author MissChen
 * @date 2024年05月15日 9:06
 */
@WebServlet("/count")
public class CountServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取session中的count数据
        Integer count= (Integer) request.getSession().getAttribute("count");

        //当第一次获取的时候，由于尚未存储，此时取出来的数据为null
        if(count==null){
            //此时设置初始值为1
            count=1;
        }else{

            //如果不是第一次，则每次在原有的基础上+1
            count++;
        }
        request.getSession().setAttribute("count",count);
        request.getRequestDispatcher("/session/count.jsp").forward(request,response);

    }
}
